package com.servicesDirectory.action.login;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.servicesDirectory.dao.login.LoginDAOImpl;

public class LoginAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;
	private String account;
    private String password;
    private String email;
    private Map<String, Object> session;
    
    public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String execute() {
        LoginDAOImpl lgDAO = new LoginDAOImpl();
        boolean result = lgDAO.checkLogin(account, password);
        String sResult;
        if (result) {
        	ActionContext.getContext().getSession().put("loginId", getAccount());
        	//session.put("loginId", account);
        	sResult = "success";
        } else {
            sResult = "error";
            addActionError("*Username/Password is invalid");
        }

        return sResult;
    }

    public void validate() {
        if (this.account.length() == 0) {
            addActionError("*Username is required");
        } else if (this.account.length() < 4) {
            addActionError("*Username is very short");
        }else if (this.account.length() > 15) {
            addActionError("*Username is too long");
        }
        if (this.password.length() == 0) {
            addActionError("*Password is required");
        }else if (this.password.length() < 4) {
            addActionError("*Password is very short");
        }else if (this.password.length() > 15) {
            addActionError("*Password is too long");
        }
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}